package com.weng.cloud.commons.base;

import java.util.Arrays;
import java.util.stream.Stream;

/**
 * @Author: weng
 * @Date: 2022/7/2
 * @Description: com.weng.cloud.commons
 */

public class StreamUtil {

    public static <T> boolean including(Stream<T> stream, T t) {
        return stream.anyMatch(ele -> ele.equals(t));
    }

    public static <T> boolean including(T[] array, T t) {
        if (ObjectUtil.isArrayNullOrEmpty(array) || ObjectUtil.isObjectNull(t)) {
            return false;
        }
        return including(Arrays.stream(array), t);
    }

    public static <T> boolean excluding(Stream<T> stream, T t) {
        return !including(stream, t);
    }

    public static <T> boolean excluding(T[] array, T t) {
        return !including(array, t);
    }

    public static Object[][] upgradeDimension(Object[] lowDimension, int rows) {
        if (ObjectUtil.isArrayNullOrEmpty(lowDimension)) {
            throw new IllegalArgumentException();
        }
        int size = lowDimension.length;
        int cols = size / rows;
        Object[][] upgradeArr =new Object[rows][cols];
        for(int idx = 0; idx < size; idx ++){
            upgradeArr[idx / cols][idx % cols] = lowDimension[idx];
        }
        return upgradeArr;
    }

    public static Object[] reduceDimension(Object[][] highDimension) {
        if (ObjectUtil.isArrayNullOrEmpty(highDimension)) {
            throw new IllegalArgumentException();
        }
        int rows = highDimension.length;
        int cols = highDimension[0].length;
        Object[] lowDimension = new Object[rows * cols];
        for (int row = 0; row < rows; row ++) {
            if (ObjectUtil.isArrayNullOrEmpty(highDimension[row])) {
                throw new IllegalArgumentException();
            }
            for(int col = 0; col < cols; col ++){
                lowDimension[row * cols + col] = highDimension[row][col];
            }
        }
        return lowDimension;
    }
}
